package com.usabb.controller;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.usabb.domain.*;
import com.usabb.enums.AmenityEnum;
import com.usabb.enums.BizTypeEnum;
import com.usabb.service.*;
import com.usabb.utils.AmenityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 民宿表
 * </pre>
 * <small> 2019-09-24 19:16:30 | Jory</small>
 */

@Slf4j
@Controller
@RequestMapping("/usabb/homeStay")
public class HomeStayController extends BaseController {
	@Autowired
	private HomeStayService homeStayService;
	@Autowired
	private AmenityService amenityService;
	@Autowired
	private AmenityRelationService amenityRelationService;
	@Autowired
	private HospitalService hospitalService;
	@Autowired
	private HospitalRelationService hospitalRelationService;
	@Autowired
	private RoundRelationService roundRelationService;
	@GetMapping()
	@RequiresPermissions("usabb:homeStay:homeStay")
	String HomeStay(){
	    return "usabb/homeStay/homeStay";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("usabb:homeStay:homeStay")
	public Result<Page<HomeStayDO>> list(HomeStayDO homeStayDO){
        Wrapper<HomeStayDO> wrapper = new EntityWrapper<>(homeStayDO).orderBy("id", false);
        Page<HomeStayDO> page = homeStayService.selectPage(getPage(HomeStayDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("usabb:homeStay:add")
	String add(){
	    return "usabb/homeStay/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("usabb:homeStay:edit")
	String edit(@PathVariable("id") Long id,Model model){
		HomeStayDO homeStay = homeStayService.selectById(id);
		model.addAttribute("homeStay", homeStay);
	    return "usabb/homestay/edit";
	}
	
	@Log("添加民宿表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("usabb:homeStay:add")
	public Result<String> save( HomeStayDO homeStay){
		homeStayService.insert(homeStay);
        return Result.ok();
	}
	
	@Log("修改民宿表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("usabb:homeStay:edit")
	public Result<String>  update( HomeStayDO homeStay){
		boolean update = homeStayService.updateById(homeStay);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除民宿表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("usabb:homeStay:remove")
	public Result<String>  remove( Long id){
		homeStayService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除民宿表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("usabb:homeStay:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		homeStayService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@GetMapping("/{baseId}/attach")
	public String goAttachPage(@PathVariable("baseId") Long baseId, Model model) {
		HomeStayDO homeStay = homeStayService.selectById(baseId);

		model.addAttribute("homeStay", homeStay);
		return "usabb/attachHomestay/attach";
	}

	@Log("民宿 配套&周边")
	@GetMapping("/amenity/{baseId}")
	public String goAmenityPage(@PathVariable("baseId") Long baseId, Model model) {
		HomeStayDO homeStay = homeStayService.selectById(baseId);
		model.addAttribute("homeStay", homeStay);

		final List<AmenityDO> amenityDOS = amenityService.selectList(null);
		AmenityUtils.append(model, amenityDOS);

		//checked items
		List<Long> relationAmenityIds = amenityRelationService.getAmenityIdListBy(BizTypeEnum.HOME_STAY.name(), baseId);
		model.addAttribute("checkedAmenityIds", relationAmenityIds);

		//round
		List<RoundRelationDO> roundRelationDOS =
				roundRelationService.getListBy(BizTypeEnum.HOME_STAY.name(), baseId);
		model.addAttribute("roundRelationDOS", roundRelationDOS);

		//hospital
		List<HospitalDO> hospitalDOS = hospitalService.selectList(null);
		model.addAttribute("hospitalDOS", hospitalDOS);
		//checked hospitals
		final List<Long> hospitalIds = hospitalRelationService.getHospitalIdsBy(BizTypeEnum.HOME_STAY.name(), baseId);
		model.addAttribute("checkedHospitalIds", hospitalIds);
		return "usabb/amenityHomeStay/add";
	}

	@Log("民宿 客人感言")
	@GetMapping("/guest/{baseId}")
	public String goGuestPage(@PathVariable("baseId") Long baseId, Model model) {
		HomeStayDO homeStay = homeStayService.selectById(baseId);

		model.addAttribute("homeStay", homeStay);
		return "usabb/guestCaseHomeStay/guestCase";
	}
}
